import { queryAllByPage } from '@/api/enumInfo'
import { mutations } from '@/store/mutations'

const state = {

}

const actions = {

  // 获取所有设备类型
  queryAllDevByPage({ commit }) {
    return new Promise((resolve, reject) => {
      queryAllByPage({
        dataStatus: 1,
        pageNum: -1
      }).then(response => {
        const { data } = response
        const enumJson = {}
        for (const i in data) {
          const enumType = data[i].enumType
          enumJson[enumType] ? enumJson[enumType].push(data[i]) : enumJson[enumType] = [data[i]]
        }
        for (const key in enumJson) {
          commit('SET_ATTR', { key: key, data: enumJson[key] })
        }
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 存设备详情数据进vuex
  saveDeviceInfo({ commit, state }, data) {
    // const randomStr = Math.random() + ''.substr(0, 9)
    // data.randomStr = randomStr
    // const commitData = {}
    // switch (data.deviceType) {
    //   case 1:
    //     // 油罐
    //     commitData.key = 'oilTank';
    //     (state.oilTank && state.oilTank.length) ? commitData.data = [...state.oilTank] : commitData.data = [data]
    //     break
    //   case 2:
    //     // 加油机
    //     break
    //   case 3:
    //     // 摄像机
    //     break
    //   case 4:
    //     // 周界防范电子围栏
    //     break
    //   case 5:
    //     // 人车识别一体机
    //     break
    //   case 6:
    //     // 汽车零发
    //     break
    //   default:
    //     break
    // }
    // commit('SET_ATTR', commitData)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
